home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13395 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  63 lines

  1. Newsgroups: comp.lang.c++,comp.lang.c
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: Include C++ in C programs
  5. Message-ID: <1996Mar25.141130.1485@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <1996Mar20.104314.28950@hgl.signaal.nl>
  11. Date: Mon, 25 Mar 1996 14:10:09 GMT
  12.  
  13. glandrup@hgl.signaal.nl (Glandrup M.H.J.) wrote:
  14.  
  15. >===============================================================================
  16. >Restriction: Unclassified
  17. >===============================================================================
  18.  
  19. >Sorry about the header, but it is policy here...
  20.  
  21.  
  22. >Hello,
  23.  
  24. >Is there anybody who knows how to call C++ member functions in a
  25. >C program? 
  26.  
  27. C++ member functions can only be called by C++ code.
  28.  
  29. In C++ it's possible to declare two functions which have the same
  30. "name" but either take different arguments, or are members of
  31. different classes (or both), and yet the compiler can distinguish
  32. them.  It does so by changing the function name ("mangling" it) so
  33. that it "somehow" includes the class name and argument types.
  34. Unfortunately, the "somehow" part is totally up to the compiler, and
  35. it may result in external names that are not representable in the
  36. character set allowed C symbol names.
  37.  
  38. What you can do is create some C++ access functions with C-callable
  39. names.
  40.  
  41. For an example, suppose you have:
  42.  
  43. class a_class
  44. {
  45.     void a_member(int);
  46. };
  47.  
  48. You can create a C-callable access function like this:
  49.  
  50. extern "C" void a_class_a_member(void *a_this, int i)
  51. {
  52.    a_class *a = (a_class *)a_this; // caller must supply "this"
  53.    a->a_member(i);
  54. }
  55.  
  56. Now your C program can call a_class_a_member().  It's up to you to
  57. figure out how to get a proper "this" pointer.
  58.  
  59. --
  60. Richard Krehbiel, Kastle Systems, Arlington VA USA
  61. rich@kastle.com (work) or richk@mnsinc.com (personal)
  62.  
  63.